﻿// -----------------------------------------------------------------------
// <copyright file="EncodingEx.cs" company="none">
// Copyright (c) 2012 M. Alberti, xanatos(at)live.it
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// </copyright>
// -----------------------------------------------------------------------

namespace FluentSerializer.Utilities
{
    using System.Text;

    /// <summary>
    /// 
    /// </summary>
    public static class EncodingEx
    {
        /// <summary>
        /// Check if an encoding is fixed length.
        /// </summary>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static bool IsFixedLength(this Encoding encoding)
        {
            return encoding.MaxCharLength() == encoding.GetByteCount(new[] { '\0' });
        }

        /// <summary>
        /// Returns the maximum byte count of a single character.
        /// </summary>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static int MaxCharLength(this Encoding encoding)
        {
            return encoding.GetMaxByteCount(1) - encoding.GetMaxByteCount(0);
        }
    }
}